﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using BLL;
using BLL.Theme;
using BLL.Formation;

namespace UI
{
    public partial class AddFormation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void bttCancel_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("~/ManageCourses.aspx");
        }

        protected void bttAdd_OnClick(object sender, EventArgs e)
        {
            FormationBO bo = new FormationBO();

            try
            {
                bo.Description = (this.formAddFormation.FindControl("tbDescription") as TextBox).Text;
                bo.Libelle = (this.formAddFormation.FindControl("tbFormationName") as TextBox).Text;
                bo.NombreJours = int.Parse((this.formAddFormation.FindControl("tbFormationsNbDeJours") as TextBox).Text);
                bo.Reference = (this.formAddFormation.FindControl("tbFormationReference") as TextBox).Text;
                
                //Get the Theme
                

                string themeSelected = (this.formAddFormation.FindControl("tbFormationTheme") as TextBox).Text;
                List<ThemeBO> themesList = ThemeBLL.GetThemes();
                ThemeBO theme = (from ThemeBO t in themesList where t.Libelle == themeSelected select t).FirstOrDefault();
                
                if (theme != null)
                {
                    bo.Theme = theme;

                    FormationBLL.AddFormation(bo);

                    Response.Redirect("~/ManageCourses.aspx");
                }
                else
                {
                    (this.formAddFormation.FindControl("errorMsgTheme") as Label).Visible = true;
                }
                    

                
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }


        }


    }
}